package com.li.configserve.config.mybatis;

import com.alibaba.druid.pool.DruidDataSource;
import com.li.configserve.utils.spring.ApplicationContextUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.wrapper.MapWrapper;

import java.util.Map;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 2022/1/17 21:25
 */
public class MapKeyLowerWrapper extends MapWrapper {

    private static final String ORACLE = "oracle";

    public MapKeyLowerWrapper(MetaObject metaObject, Map<String, Object> map) {
        super(metaObject, map);
    }

    @Override
    public String findProperty(String name, boolean useCamelCaseMapping) {
        DruidDataSource dataSource = ApplicationContextUtils.getBean(DruidDataSource.class);
        String dbType = dataSource.getDbType();
        // Oracle Map 结果中 key转为小写
        if (ORACLE.equalsIgnoreCase(dbType)) {
            return name == null ? "" : name.toLowerCase();
        }
        return name;
    }

}